package com.wuliaogame.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuliaogame.auto.JSONAuthentication;
import com.wuliaogame.entity.PApply;
import com.wuliaogame.entity.PHero;
import com.wuliaogame.entity.PUser;
import com.wuliaogame.mapper.PHeroMapper;
import com.wuliaogame.response.Result;
import com.wuliaogame.utils.UpPhotoNameUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuliaogame
 * @since 2022-04-20
 */
@RestController
@RequestMapping("/p-hero")
public class PHeroController extends JSONAuthentication {
    @Autowired
    PHeroMapper pHeroMapper;
    @ApiOperation(value = "添加英雄")
    @PostMapping("/add")
    public void addUser(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response,@RequestParam("name") String name,@RequestParam("value")String value,@RequestParam("type")String type) throws Exception {
        PHero pHero=new PHero();
        pHero.setName(name);
        pHero.setValue(value);
        pHero.setType(type);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("name", name);
        if (pHeroMapper.selectOne(wrapper) != null) {
            Result error = Result.error().message("该英雄已存在");
            this.WriteJSON(request, response, error);
        } else {
            byte[] bytes = file.getBytes();
            String imageFileName = file.getOriginalFilename();
            String fileName = UpPhotoNameUtils.getPhotoName("img", imageFileName);
            Path path = Paths.get("" + fileName);
            //“C:\\框架\\D4\\d4_pc_ui\\src\\assets\\images\\img\\”为本地目录
            Files.write(path, bytes);//写入文件
            String avatar_url = fileName;
            pHero.setPicture(avatar_url);
            response.reset();
            if (pHeroMapper.insert(pHero) > 0) {
                Result ok = Result.ok().message("添加成功");
                this.WriteJSON(request, response, ok);
            } else {
                Result error = Result.error().message("添加失败");
                this.WriteJSON(request, response, error);
            }
        }
    }
    @ApiOperation(value = "删除用户")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        int i = pHeroMapper.deleteById(id);
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "修改英雄信息，不可修改名字（可修改介绍，分路，胜率,需要带英雄id）")
    @PostMapping("/upd")
    public void upd(HttpServletRequest request, HttpServletResponse response, @RequestBody PHero pHero) throws Exception {
        PHero pHero1 = pHeroMapper.selectById(pHero.getId());
        pHero1.setValue(pHero.getValue());
        pHero1.setRate(pHero.getRate());
        pHero1.setType(pHero.getType());
        if (pHeroMapper.updateById(pHero1) > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "查看全部的英雄")
    @PostMapping("/lookAllHero")
    public void lookAllHero(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current ) throws Exception{
        Page<PHero> page=new Page<>(size,current);
        Page<PHero> page1=pHeroMapper.selectPage(page,null);
        List<PHero> records = page1.getRecords();
        int total=pHeroMapper.selectCount(null);
        if(records.size()>0){
            Result ok = Result.ok().data("hero",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "查看全部的英雄根据英雄名字")
    @PostMapping("/lookAllHeroByName")
    public void lookAllHeroByName(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current,@RequestParam("name") String name ) throws Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.like("name",name);
         Page<PHero> page=new Page<>(size,current);
        Page<PHero> page1=pHeroMapper.selectPage(page,wrapper);
        List<PHero> records = page1.getRecords();
        int total=pHeroMapper.selectCount(wrapper);
        if(records.size()>0){
            Result ok = Result.ok().data("hero",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查看全部的上单英雄")
    @PostMapping("/lookAllHeroSd")
    public void lookAllHeroSd(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current) throws Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.like("type","上单");
        Page<PHero> page=new Page<>(size,current);
        Page<PHero> page1=pHeroMapper.selectPage(page,wrapper);
        List<PHero> records = page1.getRecords();
        int total=pHeroMapper.selectCount(wrapper);
        if(records.size()>0){
            Result ok = Result.ok().data("hero",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查看全部的中单英雄")
    @PostMapping("/lookAllHeroZd")
    public void lookAllHeroZd(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current) throws Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.like("type","中单");
        Page<PHero> page=new Page<>(size,current);
        Page<PHero> page1=pHeroMapper.selectPage(page,wrapper);
        List<PHero> records = page1.getRecords();
        int total=pHeroMapper.selectCount(wrapper);
        if(records.size()>0){
            Result ok = Result.ok().data("hero",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查看全部的下路英雄")
    @PostMapping("/lookAllHeroXl")
    public void lookAllHeroXl(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current) throws Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.like("type","下路");
        Page<PHero> page=new Page<>(size,current);
        Page<PHero> page1=pHeroMapper.selectPage(page,wrapper);
        List<PHero> records = page1.getRecords();
        int total=pHeroMapper.selectCount(wrapper);
        if(records.size()>0){
            Result ok = Result.ok().data("hero",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查看全部的辅助英雄")
    @PostMapping("/lookAllHeroFz")
    public void lookAllHeroFz(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current) throws Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.like("type","辅助");
        Page<PHero> page=new Page<>(size,current);
        Page<PHero> page1=pHeroMapper.selectPage(page,wrapper);
        List<PHero> records = page1.getRecords();
        int total=pHeroMapper.selectCount(wrapper);
        if(records.size()>0){
            Result ok = Result.ok().data("hero",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查看全部的打野英雄")
    @PostMapping("/lookAllHeroDy")
    public void lookAllHeroDy(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current) throws Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.like("type","打野");
        Page<PHero> page=new Page<>(size,current);
        Page<PHero> page1=pHeroMapper.selectPage(page,wrapper);
        List<PHero> records = page1.getRecords();
        int total=pHeroMapper.selectCount(wrapper);
        if(records.size()>0){
            Result ok = Result.ok().data("hero",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查看英雄根据id")
    @PostMapping("/lookAllHeroDyById")
    public void lookAllHeroDy(HttpServletRequest request, HttpServletResponse response,@RequestParam("id") int id) throws Exception{
        PHero pHero = pHeroMapper.selectById(id);
        if(pHero!=null){
            Result ok = Result.ok().data("hero",pHero);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "英雄胜率生成表")
    @PostMapping("/HeroShenLv")
    public void HeroShenLv(HttpServletRequest request, HttpServletResponse response) throws Exception{
        Map<String,Integer> map=new HashMap<>();
        List<PHero> records = pHeroMapper.selectList(null);
        for(PHero a:records){
            map.put(a.getName(),a.getRate());
        }
        if(records.size()>0){
            Result ok = Result.ok().data("map",map);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
}

